package cn.szzsi._admin;

import cn.szzsi.common.model.Deposit;
import cn.szzsi.common.model.Pics;
import cn.szzsi.common.model.PicsItem;
import cn.szzsi.interceptor.CommonInterceptor;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.kit.ModelUtils;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 图集管理
 * Created by yuanzheng on 2016/11/30.
 */
public class PicsController extends BaseAdminController<PicsItem> {

    public void list(){
        render("list.html");
    }
    public void ajaxlist(){

        String shopId =getAttr("shopId");
        List<Pics> ls = Pics.me.find("select * from mall_pics where shopId = ? order by createtime desc",shopId);
        for(Pics s:ls){
            s.getValues();
        }
        renderJson(ls);
    }

    public void add(){
        String shopId =getAttr("shopId");
        setAttr("shopId",shopId);
        render("add.html");
    }

    public void save(){
        String shopId =getAttr("shopId");
        Pics pics = getModel(Pics.class);
        if(StrKit.notBlank(pics.getId())){
            pics.update();
            Db.update("delete from mall_pics_item where pics_id = ?",pics.getStr("id"));
        }else{
            pics.set("id",CommonUtil.getUUID()).set("shopId",shopId).set("createtime",new Date()).save();
        }
        List<PicsItem> svl = ModelUtils.batchInjectModel(getRequest(),PicsItem.class,"picsItem");
        System.out.println(svl);
        for(PicsItem sv:svl){
            sv.set("id",CommonUtil.getUUID()).set("pics_id",pics.getStr("id")).set("createtime",new Date()).save();
        }
        renderJson(Ret.ok("info","添加成功").set("url","list"));
    }

    @Clear
    public void jsonpList(){
        getResponse().setHeader("Access-Control-Allow-Origin", "*");
        String shopId =getAttr("shopId");
        String cb = getPara("callback");
        String limit = getPara("limit");

        String sql = "select * from mall_pics where shopId = ? order by createtime desc";
        if(StrKit.notBlank(limit))
            sql = sql + " limit "+limit;
        List<Pics> ls = Pics.me.find(sql,shopId);
        renderJson(cb+"("+ JsonKit.toJson(ls)+")");
    }

    @Clear
    public void jsonpItemList(){
        getResponse().setHeader("Access-Control-Allow-Origin", "*");
        String cb = getPara("callback");
        String cid = getPara("cid");
        int page = getParaToInt("page",1);
        List<PicsItem> ls = new ArrayList<PicsItem>();
        if(page == 1){
            ls = PicsItem.me.find("select * from mall_pics_item where pics_id = ? order by createtime desc limit 8",cid);
        }else{
            ls = PicsItem.me.find("select * from mall_pics_item where pics_id = ? order by createtime desc limit 8,16",cid);
        }
        renderJson(cb+"("+ JsonKit.toJson(ls)+")");
    }





    public void edit(){
        String shopId =getAttr("shopId");
        String id = getPara("id");
        Pics pics = Pics.me.findById(id);
        List<PicsItem> picsItems = PicsItem.me.find("select * from mall_pics_item where pics_id = ?",id);
        setAttr("pics",pics);
        setAttr("picsItems",picsItems);
        setAttr("nums",picsItems.size());
        setAttr("shopId",shopId);
        render("add.html");
    }

    public void del(){
        String id = getPara("id");
        Pics.me.deleteById(id);
        Db.update("delete from mall_pics_item where pics_id = ?",id);

        renderJson(Ret.ok());
    }

    public static void main(String[] args) {
        System.out.println(JsonKit.toJson(Ret.ok()));
    }


}
